package com.hymjweb.demo.hymj.flight_manage.controller;

import com.hymjweb.demo.framework.cache.bean.AMyCacheTreeNode;
import com.hymjweb.demo.framework.result.BaseResultMap;
import com.hymjweb.demo.framework.sys.BaseController;
import com.hymjweb.demo.framework.sys.SysConstant;
import com.hymjweb.demo.framework.util.DataTypeUtil;
import com.hymjweb.demo.hymj.flight_manage.bean.BFlightPojo;
import com.hymjweb.demo.hymj.flight_manage.service.BFlightService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/pages/flight_manage")
public class BFlightController extends BaseController {

    @Autowired
    private BFlightService bFlightService;

    @RequestMapping("/init")
    public String init(HttpServletRequest request) {
        return "pages/flight_manage/bflightList";
    }

    /**
     * 根据条件加载下拉航班号
     *
     * @param param
     * @return
     */
    @RequestMapping("/queryFlightNoListByDropCnd")
    @ResponseBody
    public List<Map<String, Object>> queryFlightNoListByDropCnd(@RequestBody(required = true) Map param) {
        return bFlightService.queryFlightNoListByDropCnd(param);
    }

    @RequestMapping("/query")
    @ResponseBody
    public Object query(@RequestParam(required = false) Map param) {
        return bFlightService.getPageList(param);
    }

    @RequestMapping("/findFlightNo")
    @ResponseBody
    public List<AMyCacheTreeNode> findFlightNo(@RequestParam(required = false) String airportId, String cityId, String airlinesId, String flightDate) throws Exception {
        return bFlightService.findFlightNo(airportId, cityId, airlinesId, flightDate);
    }

    @RequestMapping("/insert")
    @ResponseBody
    public Object insert(@RequestBody(required = true) BFlightPojo pojo) throws Exception {
        if (pojo == null) {
            throwException("航班添加参数错误");
        }

        BaseResultMap baseResultMap = new BaseResultMap();
        baseResultMap.setOk(true);
        baseResultMap.setMessage("航班添加成功!");

        int affectRows = bFlightService.insert(pojo);
        if (affectRows <= SysConstant.INT_ZERO) {
            baseResultMap.setOk(false);
            baseResultMap.setMessage("航班添加失败!");
        }

        return baseResultMap;
    }


    @RequestMapping("/update")
    @ResponseBody
    public Object update(@RequestBody(required = true) BFlightPojo pojo) throws Exception {
        if (pojo == null) {
            throwException("航班更新参数错误");
        }

        BaseResultMap baseResultMap = new BaseResultMap();
        baseResultMap.setOk(true);
        baseResultMap.setMessage("航班更新成功!");

        int affectRows = bFlightService.update(pojo);
        if (affectRows <= SysConstant.INT_ZERO) {
            baseResultMap.setOk(true);
            baseResultMap.setMessage("更新成功!");
        }

        return baseResultMap;
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(@RequestBody(required = true) String id) throws Exception {
        if (DataTypeUtil.isEmptyStr(id)) {
            throwException("航班删除参数错误");
        }

        BaseResultMap baseResultMap = new BaseResultMap();
        baseResultMap.setOk(true);
        baseResultMap.setMessage("航班删除成功!");

        int affectRows = bFlightService.delete(id);
        if (affectRows <= SysConstant.INT_ZERO) {
            baseResultMap.setOk(false);
            baseResultMap.setMessage("航班删除失败!");
        }

        return baseResultMap;
    }

    @RequestMapping("/batchTargetDateFlightByOriginalDate")
    @ResponseBody
    public Object batchTargetDateFlightByOriginalDate(@RequestBody(required = true) Map map) throws Exception {

        if (map == null || map.isEmpty()) {
            throwException("批量生成目标日期航班参数错误，请重试");
        }

        BaseResultMap baseResultMap = new BaseResultMap();
        baseResultMap.setOk(true);
        baseResultMap.setMessage("批量生成目标日期航班成功!");

        int affectRows = bFlightService.batchTargetDateFlightByOriginalDate(map);

        if (affectRows <= SysConstant.INT_ZERO) {
            baseResultMap.setOk(false);
            baseResultMap.setMessage("批量生成目标日期航班失败,请重试!");
        }
        return baseResultMap;
    }

    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    @ResponseBody
    public Object upload(MultipartFile upLoadFileData) throws Exception {
        if (upLoadFileData == null) {
            throwException("航班文件上传参数错误！");
        }

        BaseResultMap baseResultMap = new BaseResultMap();
        baseResultMap.setOk(true);
        baseResultMap.setMessage("航班文件上传成功!");

        int effectRow = bFlightService.upload(upLoadFileData);

        if (effectRow <= SysConstant.INT_ZERO) {
            baseResultMap.setOk(false);
            baseResultMap.setMessage("航班文件上传失败!");
        }

        return baseResultMap;
    }


}
